<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="type === '编辑'?$t('编辑'):$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'550px':'610px'" @close='close'>
    <div>
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'140px':'210px'" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('接口名称')" prop="ifname">
          <el-select v-model="record.ifname" ref="ripIfname" filterable size='small' :placeholder="$t('请选择')" style="width:280px" :disabled="type === '编辑'">
            <el-option v-for="item in ifnameList" :disabled="ripList.includes(item.strPhysicsName) || item.enPortMode !== 0 || !item.ipv4 || !item.ipv4[0].cPortIpv4" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('RIPv2认证方式')" prop="mode">
          <el-radio-group v-model="record.mode" size='small'>
            <el-radio label="md5" class="ripMd5">MD5</el-radio>
            <el-radio label="text" class="ripText">{{$t('简单密码')}}</el-radio>
            <el-radio label="off" class="ripOff">{{$t('关闭')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('MD5认证key_chain')" prop="key_chain" key="key_chain" v-if="record.mode === 'md5'">
          <template slot="label">{{$t('MD5认证key_chain')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:密钥的名字，字符串1-15个字符')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.key_chain" ref="ripKeyChain" key="ripKeyChain" style="width:280px" :placeholder="$t('请输入key_chain')" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('MD5认证key_id')" prop="key_id" key="key_id" v-if="record.mode === 'md5'">
          <template slot="label">{{$t('MD5认证key_id')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:整数，0-2147483647')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.key_id" ref="ripKeyId" key="ripKeyId" style="width:280px" :placeholder="$t('请输入key_id')" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('密码认证String')" prop="string" key="string" v-if="record.mode === 'text'">
          <template slot="label">{{$t('密码认证String')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:字符串，1-15个字符')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.string" ref="ripString" key="ripString" style="width:280px" :placeholder="$t('请输入密码')" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('水平分割')" prop="status">
          <el-radio-group v-model="record.status" size='small'>
            <el-radio label="on" class="ripStatusOn">{{$t('开启')}}</el-radio>
            <el-radio label="off" class="ripStatusOff">{{$t('关闭')}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="ripAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="ripAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { getRipIfconfig, setRipIfconfig } from '@/api/network/route/dynamicRoute/RIP'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'RipDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateKeyChain = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (String(value).length > 15) {
        callback(new Error(this.$t('长度不能超过15个字符')))
      } else {
        callback()
      }
    }
    var validateKeyId = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (value > 2147483647 || value < 0) {
        callback(new Error(this.$t('请输入0-2147483647之间的数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        mode: 'off',
        ifname: '',
        status: 'on',
        key_chain: '',
        key_id: '',
        string: ''
      },
      recordCopy: {
        mode: 'off',
        ifname: '',
        status: 'on',
        key_chain: '',
        key_id: '',
        string: ''
      },
      lang: this.$i18n.locale,
      type: '',
      ifnameList: [],
      ripList: [],
      rules: {
        ifname: [{ required: false, validator: validateName, trigger: 'blur' }],
        key_chain: [{ required: false, validator: validateKeyChain, trigger: 'blur' }],
        key_id: [{ required: false, validator: validateKeyId, trigger: 'blur' }],
        string: [{ required: false, validator: validateKeyChain, trigger: 'blur' }]
      }
    }
  },
  computed: {
    change () {
      const { mode } = this.record
      return { mode }
    }
  },
  watch: {
    change: {
      handler (newValue) {
        if (newValue.mode === 'md5') {
          this.$nextTick(() => {
            const data = ['ripKeyChain', 'ripKeyId']
            inputClass(this.$refs, data)
          })
        } else if (newValue.mode === 'text') {
          this.$nextTick(() => {
            const data = ['ripString']
            inputClass(this.$refs, data)
          })
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    init (data, type) {
      this.dialogVisible = true
      getPhysicalApiArray().then(res => { // strPhysicsName
        if (res.status === 0) {
          this.ifnameList = res.info
        }
      })
      this.ripList = []
      getRipIfconfig().then(res => { // strPhysicsName
        if (res.status === 0) {
          for (let item of res.info) {
            this.ripList.push(item.ifname)
          }
        }
      })
      this.type = type
      if (data) {
        this.record.mode = data.mode
        this.record.ifname = data.ifname
        this.record.status = data.split_horizon_status
        if (this.record.mode === 'md5') {
          this.record.key_chain = data.mode_info.key_chain
          this.record.key_id = data.mode_info.key_id
        }
        if (this.record.mode === 'text') {
          this.record.string = data.mode_info.string
        }
      }
      this.$nextTick(() => {
        const selectData = ['ripIfname']
        selectClass(this.$refs, selectData)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setRipIfconfig(this.record).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('执行成功'))
              this.close()
              this.$parent.ripConfigListInfo()
            }
          })
        }
      })
    },
    close () {
      this.type = ''
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
      this.ifnameList = []
      this.ripList = []
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
